// 作用：保留数组原有特性，重写数组部分方法
let oldArrrayProto = Array.prototype;
export let newArrayProto = Object.create(oldArrrayProto);
let methods = [
  "push",
  "pop",
  "shift",
  "unshift",
  "reverse",
  "sort",
  "splice"
]
methods.forEach(method => {
  newArrayProto[method] = function(...args) {
    const result = oldArrrayProto[method].call(this, ...args);
    // 对新增数据进行劫持
    let inserted;
    let ob = this.__ob__;
    switch(method) {
      case 'push':
      case 'unshift':
        inserted = args;
        break;
      case 'splice':
        inserted = args.slice(2);
      default:
        break;  
    }
    if(inserted) {
      ob.observeArray(inserted);
    }
    ob.dep.notify();
    return result;
  }
})
